package com.yunc.modules.cms.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrSpliter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.base.controller.CommonController;
import com.yunc.common.utils.BaseConstant;
import com.yunc.common.utils.R;
import com.yunc.modules.cms.entity.CircleContent;
import com.yunc.modules.cms.service.ICircleContentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 动弹内容发布 前端控制器
 *
 * @author concong tao
 * @since 2019-03-09
 */
@RestController
@RequestMapping("/cms/circle/content")
public class CircleContentController extends CommonController {

    @Autowired
    private ICircleContentService circleContentService;

    @GetMapping("/page")
    public R page(Page<CircleContent> page, CircleContent circleContent){
        circleContentService.page(page, new QueryWrapper<>(circleContent));
        return R.ok().put("data", page.getRecords()).put("count", page.getTotal());
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable Long id){
        CircleContent circleContent = circleContentService.getById(id);
        return R.ok().put("circleContent", circleContent);
    }

    @PostMapping("/save")
    public R save(@RequestBody CircleContent circleContent){
        circleContent.setCreator(getUserId());
        circleContent.setCreateTime(DateUtil.date());
        circleContent.setRemoveTag(BaseConstant.REMOVE_N);
        boolean flag = circleContentService.save(circleContent);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    @PutMapping(value = "/update")
    public R update(@RequestBody CircleContent circleContent){
        boolean flag = circleContentService.updateById(circleContent);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable String ids){
        if (StringUtils.isEmpty(ids)) {
            return R.error("传入值为空");
        }
        List<String> idsList = StrSpliter.split(ids, ',', 0, true, true);
        if (CollUtil.isEmpty(idsList)) {
            return R.error("传入值格式错误");
        }
        boolean flag = circleContentService.removeByIds(idsList);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }
}
